package com.bing.utils;

import com.bing.bean.weixin.*;

import java.util.List;

/**
 * Created by kaibb on 2014/12/6.
 */
public final class MsgHelper {

    /*
     <xml>
     <ToUserName><![CDATA[toUser]]></ToUserName>
     <FromUserName><![CDATA[fromUser]]></FromUserName>
     <CreateTime>12345678</CreateTime>
     <MsgType><![CDATA[text]]></MsgType>
     <Content><![CDATA[你好]]></Content>
     </xml>
     */
    public static ResponseMessage toText(RequestMessage requestMessage, String content) {
        ResponseMessage responseMessage = new ResponseMessage();
        responseMessage.setMsgType(MsgType.text);
        baseInfo(requestMessage, responseMessage);
        responseMessage.setContent(content);
        return responseMessage;
    }


    /**
     * <xml>
     * <ToUserName><![CDATA[toUser]]></ToUserName>
     * <FromUserName><![CDATA[fromUser]]></FromUserName>
     * <CreateTime>12345678</CreateTime>
     * <MsgType><![CDATA[news]]></MsgType>
     * <ArticleCount>2</ArticleCount>
     * <Articles>
     * <item>
     * <Title><![CDATA[title1]]></Title>
     * <Description><![CDATA[description1]]></Description>
     * <PicUrl><![CDATA[picurl]]></PicUrl>
     * <Url><![CDATA[url]]></Url>
     * </item>
     * <item>
     * <Title><![CDATA[title]]></Title>
     * <Description><![CDATA[description]]></Description>
     * <PicUrl><![CDATA[picurl]]></PicUrl>
     * <Url><![CDATA[url]]></Url>
     * </item>
     * </Articles>
     * </xml>
     *
     * @param requestMessage
     * @param articles
     * @return responseMessage
     */
    public static ResponseMessage toNews(RequestMessage requestMessage, List<Article> articles) {
        ResponseMessage responseMessage = new ResponseMessage();
        responseMessage.setMsgType(MsgType.news);
        baseInfo(requestMessage, responseMessage);
        responseMessage.setArticles(articles);
        responseMessage.setArticleCount(articles.size());
        return responseMessage;
    }

    private static void baseInfo(RequestMessage requestMessage, ResponseMessage responseMessage) {
        responseMessage.setToUserName(requestMessage.getFromUserName());
        responseMessage.setFromUserName(requestMessage.getToUserName());
        responseMessage.setCreateTime(System.currentTimeMillis() / 1000);
    }

    /**
     * <xml>
     * <ToUserName><![CDATA[toUser]]></ToUserName>
     * <FromUserName><![CDATA[fromUser]]></FromUserName>
     * <CreateTime>12345678</CreateTime>
     * <MsgType><![CDATA[image]]></MsgType>
     * <Image>
     * <MediaId><![CDATA[media_id]]></MediaId>
     * </Image>
     * </xml>
     *
     * @param requestMessage
     * @return responseMessage
     */
    public static ResponseMessage toImage(RequestMessage requestMessage) {
        ResponseMessage responseMessage = new ResponseMessage();
        responseMessage.setMsgType(MsgType.image);
        baseInfo(requestMessage, responseMessage);
        Media media = new Media();
        media.setMediaId(requestMessage.getMediaId());
        responseMessage.setImage(media);
        return responseMessage;
    }

    public static void toMsg(ResponseMessage responseMessage, RequestMessage requestMessage) {
        responseMessage.setFromUserName(requestMessage.getToUserName());
        responseMessage.setToUserName(requestMessage.getFromUserName());
        responseMessage.setCreateTime(System.currentTimeMillis());
    }
}
